import Vue from "vue";
import store from "@/store";
import router, { constantRouterMap } from "@/router";
// import  from '@/router';
import { getToken } from "@/utils/auth"; // get token from cookie

// socket连接
import * as socketApi from "@/api/socket";
Vue.prototype.socketApi = socketApi;

function selectCom(directer) {
  switch (directer) {
    // case 'merchant/index':
    //   return () =>
    //     import('@/views/base-data/merchant');
    // case 'ad/index':
    //   return () => import('@/views/base-data/ad');
    // case 'itoc/index':
    //   return () => import('@/views/base-data/itoc');
    // case 'region/index':
    //   return () => import('@/views/base-data/region');
    // case 'location':
    //   return () => import('@/views/base-data/location');
    // case 'trade/index':
    //   return () => import('@/views/base-data/trade');
    // case 'admin/index':
    //   return () => import('@/views/admin');
    // case 'admin/edit':
    //   return () => import('@/views/admin/edit');
    // case 'equipmentRequest/index':
    //   return () => import('@/views/equipment/registerList');
    // case 'equipment/list':
    //   return () => import('@/views/equipment/list');
    // case 'Centra/getRequestList':
    //   return () => import('@/views/centra/getRequestList');
    // case 'Centra/getList':
    //   return () => import('@/views/centra/getList');
    // case 'adVideo/getRequestList':
    //   return () => import('@/views/video/getRequestList');
    // case 'adVideo/index':
    //   return () => import('@/views/video');
    // case 'adPlayRule/getRequestList':
    //   return () => import('@/views/play-rule/getRequestList');
    // case 'adPlayRule/getList':
    //   return () => import('@/views/play-rule/getList');
    // case 'playRule/addRule':
    //   return () => import('@/views/play-rule/addRule');
    // case 'equipmentLog/index':
    //   return () => import('@/views/equipment/equipmentLog');
    // case 'log/index':
    //   return () => import('@/views/equipment/log');
    // case 'PlayLog/index':
    //   return () => import('@/views/equipment/playLog');
    // case 'adPlayLog/index':
    //   return () => import('@/views/equipment/adPlayLog');
    // case 'maintain/index':
    //   return () => import('@/views/maintenance');
    // case 'AuthRule/index':
    //   return () => import('@/views/authRule');
    // case 'coupon/index':
    //   return () => import('@/views/coupon');
    // case 'adOrder/index':
    //   return () => import('@/views/coupon/adOrder');
    // case 'videoData/index':
    //   return () => import('@/views/videoData');
    // case 'videoPlayData/list':
    //   return () => import('@/views/videoData/videoPlayData');
    // case 'videoPlayData/index':
    //   return () => import('@/views/videoData/videoPlaylist');
    // case 'index':
    //   return () => import('@/views/index');
    // case 'broadcast/index':
    //   return () => import('@/views/broadcast');
    // case 'goodsCategory/index':
    //   return () => import('@/views/goods/goodsCategory');
    // case 'goods/index':
    //   return () => import('@/views/goods');
    // case 'order/index':
    //   return () => import('@/views/order');
    // case 'recharge':
    //   return () => import('@/views/order/recharge');
    // case 'order/orderList':
    //   return () => import('@/views/order/orderList');
    // case 'upload/index':
    //   return () => import('@/views/equipment/equipmentUpload');
    // case 'EquipmentScreen/index':
    //   return () => import('@/views/equipment/equipmentScreen');
    // case 'cost/index':
    //   return () => import('@/views/cost');
    // case 'CentraScreen/index':
    //   return () => import('@/views/centra');
    // case 'CentraScreen/index':
    //   return () => import('@/views/centra');
    // case 'wx/index':
    //   return () => import('@/views/admin/weChat');
    default:
      return () => import("@/views/errorPage/404");
  }
}
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    jurisdiction: ""
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = constantRouterMap.concat(routers);
      router.addRoutes(state.routers);
    },
    JURISDICTION: (state, jurisdiction) => {
      console.log("jurisdiction", jurisdiction);
      state.jurisdiction = jurisdiction;
    }
  },
  actions: {
    GenerateRoutes({ commit }, routerPath) {
      const data = {
        returnType: "111"
      };
      // console.log('刷新页面',JSON.parse(getToken()).token)
      if (JSON.parse(getToken()).token) {
        data["token"] = JSON.parse(getToken()).token;
      }
      const getMenus = {
        company: "STQ",
        api: "pc_home_index",
        version: "1.0",
        data: data
      };
      let asyncRouterMap = [];
      console.log(JSON.stringify(getMenus));
      socketApi.sendSock(JSON.stringify(getMenus), res => {
        if (res && res !== '{"type":"ping"}') {
          // 判断返回值是否是有效返回值(不为websockit心跳)
          const resj = JSON.parse(res);
          console.log("resj", resj);
          let jurisdiction = "";
          if (resj.code !== -1) {
            jurisdiction = resj.message;
            asyncRouterMap = resj.data;
          } else {
            // token 过期
            asyncRouterMap = [];
            console.log("token 过期");
            store.dispatch("LogOut");
          }
          const ruterMaps = menuMap(asyncRouterMap);
          commit("SET_ROUTERS", ruterMaps);
          commit("JURISDICTION", jurisdiction);
          // console.log('routerPath11', routerPath)
          // router.push({ path: routerPath });
        }
      });
    }
  }
};

function menuMap(res, menu = 1) {
  // 递归函数生成菜单
  const ruterMaps = [];
  res.forEach(element => {
    const ruterObjects = {};
    if (element.name === "index") {
      ruterObjects.path = "/index";
      ruterObjects.component = () => import("@/layout");
      const onePage = {};
      onePage.path = element.name;
      onePage.component = selectCom(element.name);
      onePage.name = element.name;
      onePage.meta = {
        title: element.title,
        icon: element.icon
      };
      ruterObjects.children = [onePage];
    } else {
      if (element.ismenu === 1) {
        if (element.type === 1) {
          console.log("element333", element);
          ruterObjects.hidden = true;
        }
        if (menu === 1) {
          ruterObjects.alwaysShow = true;
          ruterObjects.path = element.name;
          ruterObjects.component = () => import("@/layout");
        } else {
          ruterObjects.path = "/" + element.name;
          ruterObjects.component = selectCom(element.name);
        }
        ruterObjects.name = element.name;
        ruterObjects.meta = {
          title: element.title,
          icon: element.icon
        };
        if (element.children) {
          if (menuMap(element.children).length > 0) {
            ruterObjects.children = menuMap(element.children, 0);
          }
        }
      }
    }
    if (Object.keys(ruterObjects).length) {
      ruterMaps.push(ruterObjects);
    }
  });
  // console.log('ruterMaps', ruterMaps)
  return ruterMaps;
}

export default permission;
